#include <stdio.h>
#include <dlfcn.h>

int main(int argc, char *argv[])
{
    int ret;
    if(argc < 6)
    {
        fprintf(stderr, "Usage: scelmaker-test <scelmaker dll> <pinyin> <unicode2pinyin> <input> <output> \n");
        return -1;
    }
    typedef int (*SCELMAKER_PTR)(const char *, const char*);
    typedef void(*SCELDESTROY_PTR)(void);
    
    auto dlhandler = dlopen(argv[1], RTLD_LAZY);
    const char *per = NULL;
    if(NULL == dlhandler && (per = dlerror()))
    {
        fprintf(stderr, "Open DLL [%s] failed:%s", argv[1], per);
        return -2;
    }
    auto scel_init = (SCELMAKER_PTR)dlsym(dlhandler, "scel_init");
    if(NULL == scel_init && (per = dlerror()))
    {
        fprintf(stderr, "Get sym [scel_init] failed:%s", per);
        return -3;
    }
    if(0 != (ret = scel_init(argv[2], argv[3])))
    {
        fprintf(stderr, "scel_init failed, error code:%d", ret);
        return ret;
    }
    auto scel_make = (SCELMAKER_PTR)dlsym(dlhandler, "scel_make");
    if(NULL == scel_make && (per = dlerror()))
    {
        fprintf(stderr, "Get sym [scel_make] failed:%s", per);
        return -4;
    }
    ret = scel_make(argv[4], argv[5]);
    auto scel_destroy = (SCELDESTROY_PTR)dlsym(dlhandler, "scel_destroy");
    if(NULL == scel_destroy && (per = dlerror()))
    {
        fprintf(stderr, "Get sym [scel_destroy] failed:%s", per);
        return -5;
    }
    scel_destroy();
    return ret;    
}
